<?php
/**
 * Anniversary.php
 * 文件描述 纪念日控制器
 * Created on 2020/8/31 11:46
 * Created by 刘小龙 595929049@qq.com
 */

namespace app\xiangji\controller;


use app\Request;
use app\xiangji\BaseController;
use app\common\helpers\Response;
use app\xiangji\services\DummyService;
use think\App;
use think\exception\ValidateException;

use app\common\validates\Dummy as DummyValidate;

class Dummy extends BaseController
{
    private $dummyService;

    public function __construct(DummyService $dummyService)
    {
        $this->dummyService = $dummyService;
    }

    /**
     * @todo 获取虚拟对象信息
     * @return \think\response\Json
     */
    public function show()
    {
        $result = $this->dummyService->show();
        return $this->success($result);
    }

    /**
     * @todo 创建虚拟对象
     * @param Request $request
     * @return \think\response\Json
     */
    public function store(Request $request)
    {
        try {
            // 校验数据
            validate(DummyValidate::class)->scene('store')->check($request->post());
            // 逻辑处理
            $result = $this->dummyService->store($request);
            return $this->success($result);
        }catch (ValidateException $exception){  // 数据校验未通过，返回错误信息
            return $this->failed($exception->getError(), Response::HTTP_BAD_REQUEST);
        }
    }

    /**
     * @todo 更新虚拟对象信息
     * @param Request $request
     * @param $id
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        try {
            // 校验数据
            validate(DummyValidate::class)->scene('update')->check($request->put());
            // 逻辑处理
            $result = $this->dummyService->update($request);
            return $this->success($result);
        }catch (ValidateException $exception){  // 数据校验未通过，返回错误信息
            return $this->failed($exception->getError(), Response::HTTP_BAD_REQUEST);
        }
    }
}
